<template>
  <div id="homePage">
    <!-- 搜索框和筛选部分保持不变 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图库中搜索"
        enter-button="搜索"
        size="large"
        @search="doSearch"
        allow-clear
      />
    </div>
    <!--    分类和标签筛选-->
    <a-tabs v-model:active-key="selectdCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :tab="category" :key="category" />
    </a-tabs>
    <div class="tab-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectdTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>

    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" />
    <!-- 分页组件 -->
    <a-pagination
      style="text-align: right; margin-top: 20px"
      v-model:current="searchParams.current"
      v-model:page-size="searchParams.pageSize"
      :total="total"
      @change = "onPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue'

//定义数据
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(true)

//搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})

//分页参数
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}
//页面加载时获取数据
onMounted(() => {
  fetchData()
})

//获取数据
const fetchData = async () => {
  loading.value = true
  //转换搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectdCategory.value !== 'all') {
    params.category = selectdCategory.value
  }
  selectdTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
  loading.value = false
}

//搜索
const doSearch = () => {
  // 重置分页参数
  searchParams.current = 1
  fetchData()
}

//标签和分类列表
const categoryList = ref<string[]>([])
const tagList = ref<string[]>([])
const selectdCategory = ref<string>('all')
const selectdTagList = ref<boolean[]>([])

/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取标签分类列表失败失败' + res.data.message)
  }
}


onMounted(() => {
  getTagCategoryOptions()
})
</script>
<style scoped>
#homePage .search-bar {
  max-width: 560px;
  margin: 0 auto 20px;
}

#homePage .tab-bar {
  margin-bottom: 16px;
}

.picture-card {
  position: relative;
  overflow: hidden;
  /* 调整为长条比例，高度大于宽度 */
  aspect-ratio: 3 / 4;
  border-radius: 10px; /* 添加圆角 */
}

.picture-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px; /* 图片也添加圆角 */
}

.picture-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.6) 100%);
  color: white;
  padding: 10px;
  opacity: 0;
  transition: opacity 0.3s ease;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  border-radius: 10px; /* 遮罩也添加圆角 */
}

.picture-card:hover .picture-overlay {
  opacity: 1;
}

.overlay-content {
  width: 100%;
}

.overlay-content h3 {
  margin: 0 0 5px 0;
  color: #ffffff;
}
</style>
